package com.heatingcharge.controller.base;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.heatingcharge.common.util.R;
import com.heatingcharge.dto.HeatYearDTO;
import com.heatingcharge.entity.base.HeatYear;
import com.heatingcharge.param.HeatYearParam;
import com.heatingcharge.service.base.IHeatYearService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author devin.jiang
 * @since 2019-10-13
 */
@RestController
@RequestMapping("/heat-year")
public class HeatYearController {
    @Autowired
    IHeatYearService heatYearService;

    @GetMapping
    public R getHeatYear(HeatYearDTO heatYearDTO){
        QueryWrapper<HeatYear> queryWrapper=new QueryWrapper();
        if (ObjectUtil.isNotEmpty(heatYearDTO.getYearName())) {
            queryWrapper.like("year_name", heatYearDTO.getYearName());
        }
        queryWrapper.orderByDesc("year_name");
        return R.ok(heatYearService.list(queryWrapper));
    }

    @PostMapping
    public R save(@RequestBody HeatYearParam heatYearParam){
        if (heatYearService.save(heatYearParam)>0){
            return R.ok();
        }
        return R.error();
    }

    @PutMapping
    public R update(@RequestBody HeatYearParam heatYearParam){
        if (heatYearService.update(heatYearParam)>0){
            return R.ok();
        }
        return R.error();
    }

    @DeleteMapping("/{id}")
    public R delete(@PathVariable("id") Integer id){
        if (heatYearService.removeById(id)){
            return R.ok();
        }
        return R.error();
    }
}

